package com.wnhz.work.jy.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.opengl.EGLDisplay;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EdgeEffect;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.solidfire.gson.Gson;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.MyMain;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.activity.ForgetActivity;
import com.wnhz.work.jy.activity.LoginActivity;
import com.wnhz.work.jy.bean.UserBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * ----登录
 */
public class LoginFragment extends Fragment implements View.OnClickListener {
    private View view;
    private TextView tv_agreement;
    private IWXAPI api;
    private String APP_ID = "wx054406f5ccd828da";

    private EditText et_phone;
    private EditText et_passWord;

    private String phoneNumber;
    private String password;
    private LoginActivity activity;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_login, container, false);
        activity = (LoginActivity) getActivity();
        initView();
        return view;
    }

    private void initView() {

        tv_agreement = (TextView) view.findViewById(R.id.tv_agreement);
        et_phone = (EditText) view.findViewById(R.id.et_phone);
        et_passWord = (EditText) view.findViewById(R.id.et_passWord);

        SpannableString text = new SpannableString("*注册/登录表示你已同意《嘉轶金服服务协议》");
        text.setSpan(new ForegroundColorSpan(0xffff3c3d), 12, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_agreement.setText(text);
        tv_agreement.setOnClickListener(this);
        view.findViewById(R.id.tv_login).setOnClickListener(this);
        view.findViewById(R.id.iv_weixin).setOnClickListener(this);
        view.findViewById(R.id.iv_weibo).setOnClickListener(this);
        view.findViewById(R.id.iv_qq).setOnClickListener(this);
        view.findViewById(R.id.tv_forget_password).setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        //activity.closeDialog();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            //登录
            case R.id.tv_login:
                phoneNumber = et_phone.getText().toString();
                password = et_passWord.getText().toString();
                /*Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
                UserBean userBean = new UserBean();
                userBean.setUserId("106");
                MyApplication.getInstance().userBean = userBean;
                startActivity(new Intent(getActivity(), MyMain.class));*/
                if (!MyUtils.isMobileNO(phoneNumber)){
                    Toast.makeText(getActivity(),"请输入正确手机号",Toast.LENGTH_LONG).show();
                }else if ("".equals(password) || password == null){
                    Toast.makeText(getActivity(),"请输入密码",Toast.LENGTH_LONG).show();
                }else {
                    login(phoneNumber,password);
                }

                break;

            //微信登录
            case R.id.iv_weixin:
//                Toast.makeText(getActivity(), "嘿嘿嘿", Toast.LENGTH_SHORT).show();
                activity.showDialog();
                if (api == null) {
                    api = WXAPIFactory.createWXAPI(getActivity(), APP_ID, true);
                    api.registerApp(APP_ID);
                }
                if (!api.isWXAppInstalled()) {
                    //提醒用户没有按照微信
                    Toast.makeText(getActivity(), "没有安装微信,请先安装微信!", Toast.LENGTH_SHORT).show();
                    return;
                }

                /*SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "wechat_sdk_demo_test";
                api.sendReq(req);*/
                WXTextObject textObject = new WXTextObject();
                textObject.text = "text";
                WXMediaMessage message = new WXMediaMessage();
                message.mediaObject = textObject;
                message.description = "text";

                final SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "wechat_sdk_demo_test";

                api.sendReq(req);
                break;

            //忘记密码
            case R.id.tv_forget_password:
                intent = new Intent(getActivity(), ForgetActivity.class);
                startActivity(intent);
                break;

            /*//微博登录
            case R.id.iv_weibo:
                Toast.makeText(getActivity(), "微博登录", Toast.LENGTH_SHORT).show();
                break;*/

           /* //QQ登录
            case R.id.iv_qq:
                Toast.makeText(getActivity(), "QQ登录", Toast.LENGTH_SHORT).show();
                break;*/

            case R.id.tv_agreement:
                activity.agreement();
                break;
            default:
                break;
        }
    }

    private static final String TAG = "LoginFragment";
    private void login(final String phoneNumber, final String password){
        Map<String,Object> map = new HashMap<>();
        map.put("phoneNumber",phoneNumber);
        map.put("password",password);
        activity.showDialog();
        XUtil.Post(ConfigUtil.LOGIN,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result );
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    if ("1".equals(re)){
                        String r = jsonObject.optString("info");
                        UserBean userBean = new UserBean();
                        userBean = new Gson().fromJson(r,UserBean.class);
                        MyApplication.getInstance().userBean = userBean;
                        MyApplication.getInstance().addUser(phoneNumber,password);
                        Log.e(TAG, "onSuccess: "+userBean.toString() );
                        startActivity(new Intent(getActivity(), MyMain.class));
                    }else {
                        String info = jsonObject.optString("info");
                        if (info != null && !"".equals(info)){
                            Toast.makeText(getActivity(),info,Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                Log.e(TAG, "onError: "+ex);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                activity.closeDialog();
            }
        });
    }
}
